package cc.vicp.djx314.starfood.web.count;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.service.count.CountService;

@Controller
public class DishBuyWeekTopController {
	public static final Logger logger = LoggerFactory.getLogger(DishBuyWeekTopController.class);

	@Autowired
	CountService countService;

	@Deprecated
	public ModelAndView dishBuyWeekTopDeprecated() {
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		boolean success = false;
		List<Dish> dishes = null;
		try {
			Date today = new Date();
			Date lastWeek = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
			Calendar cal = Calendar.getInstance();
			cal.setTime(lastWeek);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			Date lastWeekDate = cal.getTime();
			dishes = countService.getTopDishByWeek(lastWeekDate);
			success = true;
		} catch (Exception e) {
			logger.info("获取上周菜式销售前10信息出错", e);
		}
		model.addObject("dishes", dishes);
		model.addObject("success", success);
		return model;
	}

	@RequestMapping(value = "/manager/dish/week/top", method = RequestMethod.GET)
	public ModelAndView dishBuyWeekTop() {
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		boolean success = false;
		List<Dish> dishes = null;
		try {
			Date today = new Date();
			dishes = countService.getTopDishLastWeek(today);
			success = true;
		} catch (Exception e) {
			logger.info("获取上周菜式销售前10信息出错", e);
		}
		model.addObject("dishes", dishes);
		model.addObject("success", success);
		return model;
	}
}